/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import DAO.DAO_Ga;
import DBTransmit.TableGaRecord;
import java.util.List;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
import ticketbooking.Utils;

/**
 *
 * @author LEHOANGKHANH
 */
public class QLGa extends javax.swing.JInternalFrame {

    /**
     * Creates new form QLGa_
     */
    SecretKey secretKey = null;
    public QLGa(SecretKey key) {
        initComponents();
        this.secretKey = key;
        InitTable();
    }
    public void InitTable(){
        List<TableGaRecord> lst = DAO_Ga.LoadDS(secretKey);
        Utils.LoadDataToTable(jTable1, lst);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tenGa = new javax.swing.JTextField();
        maGa = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        diaDiem = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        them = new javax.swing.JButton();
        sua = new javax.swing.JButton();
        xoa = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        ok = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Quản Lý Ga");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Địa Điểm:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Tên Ga:");

        tenGa.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        maGa.setEditable(false);
        maGa.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        maGa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maGaActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Mã Ga:");

        diaDiem.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Mã Ga", "Tên Ga", "Địa Điểm"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        them.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/add.png"))); // NOI18N
        them.setText("Thêm");
        them.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                themActionPerformed(evt);
            }
        });

        sua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/modify.png"))); // NOI18N
        sua.setText("Sửa");
        sua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                suaActionPerformed(evt);
            }
        });

        xoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/erase.png"))); // NOI18N
        xoa.setText("Xóa");
        xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xoaActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 204));
        jLabel1.setText("Quản Lý Ga");

        ok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IMG/1403367972_clean.png"))); // NOI18N
        ok.setEnabled(false);
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(127, 127, 127))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(them)
                        .addGap(18, 18, 18)
                        .addComponent(sua)
                        .addGap(18, 18, 18)
                        .addComponent(xoa)
                        .addGap(46, 46, 46))))
            .addGroup(layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tenGa)
                    .addComponent(maGa)
                    .addComponent(diaDiem, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(ok, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(maGa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2))
                    .addComponent(ok))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tenGa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(diaDiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(them)
                    .addComponent(sua)
                    .addComponent(xoa))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void themActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_themActionPerformed
        // TODO add your handling code here:
        maGa.setText("");
        tenGa.setText("");
        diaDiem.setText("");
        maGa.setEditable(true);
        sua.setEnabled(false);
        ok.setEnabled(true);
    }//GEN-LAST:event_themActionPerformed

    private void maGaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maGaActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_maGaActionPerformed

    private void suaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_suaActionPerformed
        // TODO add your handling code here:
        TableGaRecord record = new TableGaRecord();
        record.setMaGa(maGa.getText());
        record.setTenGa(tenGa.getText());
        record.setDiaDiem(diaDiem.getText());
        boolean state = DAO_Ga.Update(record, secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Sửa dữ liệu thành công!");
            InitTable();
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Sửa dữ liệu thất bại");
        }
    }//GEN-LAST:event_suaActionPerformed

    private void xoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xoaActionPerformed
        // TODO add your handling code here:
        boolean state = DAO_Ga.Delete(maGa.getText(), secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Xóa dữ liệu thành công!");
            InitTable();
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Xóa dữ liệu thất bại!");
        }
    }//GEN-LAST:event_xoaActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        // TODO add your handling code here:
        if(maGa.getText().equals("")){
            JOptionPane.showMessageDialog(rootPane, "Chưa nhập mã ga!");
            return;
        }
        TableGaRecord record = new TableGaRecord();
        record.setMaGa(maGa.getText());
        record.setTenGa(tenGa.getText());
        record.setDiaDiem(diaDiem.getText());
        boolean state = DAO_Ga.Add(record, secretKey);
        if(state == true){
            JOptionPane.showMessageDialog(rootPane, "Thêm dữ liệu thành công!");
            maGa.setEditable(false);
            sua.setEnabled(true);
            ok.setEnabled(false);
            InitTable();
        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Thêm dữ liệu thất bại!");
            maGa.setEditable(false);
            sua.setEnabled(true);
            ok.setEnabled(false);
        }  
    }//GEN-LAST:event_okActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int rowIndex = jTable1.getSelectedRow();
        if(rowIndex == -1){
            return;
        }
        maGa.setText(jTable1.getValueAt(rowIndex,0).toString());
        tenGa.setText(jTable1.getValueAt(rowIndex,1).toString());
        diaDiem.setText(jTable1.getValueAt(rowIndex,2).toString());
    }//GEN-LAST:event_jTable1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField diaDiem;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField maGa;
    private javax.swing.JButton ok;
    private javax.swing.JButton sua;
    private javax.swing.JTextField tenGa;
    private javax.swing.JButton them;
    private javax.swing.JButton xoa;
    // End of variables declaration//GEN-END:variables
}
